package com.xusheng.cli.controller.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xusheng.cli.common.annotation.OperationLogAnno;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.enums.OperationType;
import com.xusheng.cli.common.validation.Save;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.dictionaryModule.Category;
import com.xusheng.cli.entity.favoritesModule.Favorites;
import com.xusheng.cli.entity.videoModule.Video;
import com.xusheng.cli.req.FavoritesQueryReq;
import com.xusheng.cli.service.FavoritesService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 收藏夹接口
 *
 * @author lyh
 * @since 2023-04-27 17:23:53
 */
@Validated
@RestController
@RequestMapping("/user/v1/favorites")
public class FavoritesController {
    /**
     * 服务对象
     */
    @Resource
    private FavoritesService favoritesService;

    /**
     * 查询所有数据
     *
     * @param favorites 查询实体
     * @return 所有数据
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @GetMapping("queryAll")
    public Result<List<Favorites>> queryAll(Favorites favorites) {
        long userId = CurrentPlayerUserUtil.getUserId();
        favorites.setUserId(userId);
        return Result.success(favoritesService.list(new QueryWrapper<>(favorites)));
    }

    /**
     * 分页查询所有数据
     *
     * @param req 查询条件
     * @return 所有数据
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @GetMapping("queryByPage")
    public Result<MyPage<Video>> queryByPage(FavoritesQueryReq req) {
        long userId = CurrentPlayerUserUtil.getUserId();
        req.setUserId(userId);
        MyPage<Video> page = favoritesService.favoritesPage(req, false);
        page.getRecords().forEach(v -> {
            if (v.getSource() == 0) v.setUrl(null);
        });
        return Result.success(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @GetMapping("queryOne/{id}")
    public Result<Video> selectOne(@PathVariable Serializable id) {
        JoinLambdaWrapper<Favorites> wrapper = new JoinLambdaWrapper<>(Favorites.class);
        wrapper.eq(Favorites::getId, id)
                .leftJoin(Video.class, Video::getId, Favorites::getVideoId)
                .joinAnd(Video::getDeleted, false, 0)
                .end();
        Video video = favoritesService.joinGetOne(wrapper, Video.class);
        if (video != null && video.getSource() == 0) video.setUrl(null);
        return Result.success(video);
    }

    /**
     * 新增数据
     *
     * @param favorites 实体对象
     * @return 新增结果
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @PostMapping("insert")
    @OperationLogAnno(module = "收藏夹模块", type = OperationType.CREATE, desc = "添加一条收藏夹数据")
    public Result<Object> insert(@Validated(Save.class) @RequestBody Favorites favorites) {
        long userId = CurrentPlayerUserUtil.getUserId();
        favorites.setUserId(userId);
        long count = favoritesService.count(Wrappers.<Favorites>lambdaQuery()
                .eq(Favorites::getVideoId, favorites.getVideoId())
                .eq(Favorites::getUserId, userId));
        if (count > 0) return Result.error("该视频已被收藏，请勿重复收藏");
        return Result.isSuccess(favoritesService.save(favorites));
    }

    /**
     * 修改数据
     *
     * @param favorites 实体对象
     * @return 修改结果
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @PutMapping("update")
    @OperationLogAnno(module = "收藏夹模块", type = OperationType.UPDATE, desc = "修改一条收藏夹数据")
    public Result<Object> update(@Validated(Update.class) @RequestBody Favorites favorites) {
        return Result.isSuccess(favoritesService.updateById(favorites));
    }

    /**
     * 删除单条数据
     *
     * @param id 主键
     * @return 删除结果
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @DeleteMapping("delete/{id}")
    @OperationLogAnno(module = "收藏夹模块", type = OperationType.UPDATE, desc = "删除一条收藏夹数据")
    public Result<Object> delete(@PathVariable Long id) {
        long userId = CurrentPlayerUserUtil.getUserId();
        return Result.isSuccess(favoritesService.remove(Wrappers.<Favorites>lambdaQuery()
                .eq(Favorites::getVideoId, id)
                .eq(Favorites::getUserId, userId)));
    }

    /**
     * 批量删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     * @author lyh
     * @since 2023-04-27 17:23:53
     */
    @DeleteMapping("batchDelete")
    @OperationLogAnno(module = "收藏夹模块", type = OperationType.UPDATE, desc = "批量删除收藏夹数据")
    public Result<Object> batchDelete(@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
        long userId = CurrentPlayerUserUtil.getUserId();
        return Result.isSuccess(favoritesService.remove(Wrappers.<Favorites>lambdaQuery()
                .in(Favorites::getVideoId, idList)
                .eq(Favorites::getUserId, userId)));
    }

    /**
     * 查询当前用户收藏所对应的分类
     *
     * @return com.xusheng.cli.common.base.Result<java.util.List < com.xusheng.cli.entity.dictionaryModule.Category>>
     * @author 王鹤然
     * @date 2023/5/16 0016 下午 14:20
     */
    @GetMapping("/category")
    public Result<List<Category>> categoryByCollect() {
        return Result.success(favoritesService.categoryByCollect());
    }

    /**
     * 根据分类查询收藏
     *
     * @return com.xusheng.cli.common.base.Result<java.util.List < com.xusheng.cli.entity.dictionaryModule.Category>>
     * @author 王鹤然
     * @date 2023/5/16 0016 下午 14:33
     */
    @GetMapping("/listByCategory")
    public Result<List<Video>> listByCategory(Long id) {
        return Result.success(favoritesService.listByCategory(id));
    }
}

